/**
 * 这里存放的是对Product的操作。
 */
import { ajaxTcbCloud } from "@/utils/cloud";
import {
  successResponseType,
  failRejectType,
  productCollectionType
} from "CustomTypes";

/**
 * 获取一级（parentId===0）、二级(parentId!==0)分类列表
 * 这个parentId如果是'0'，必须变成数字0否则得不到数据。
 */
export const reqCategorys = (parentId = "0", flag: boolean) =>
  ajaxTcbCloud("/manage/category/list", { parentId }, flag);
/**
 * 搜索单个商品信息
 */
export const reqProductInfo = (_id: string) =>
  ajaxTcbCloud("/manage/product/searchOne", { _id });
/**
 * 获取商品的分页列表
 *
 *  @param pageNum 表示哪一页
 * @param pageSize 每一页显示的数据数量
 */
export const reqProducts = (
  pageNum = 1,
  pageSize = 1
): Promise<successResponseType | failRejectType> =>
  ajaxTcbCloud("/manage/product/list", {
    pageNum,
    pageSize
  });
/**
 * 获取/查找数据
 */
export const reqSearchProducts = ({
  pageNum,
  pageSize,
  searchName,
  searchType
}: any): Promise<successResponseType | failRejectType> =>
  ajaxTcbCloud("/manage/product/search", {
    pageNum,
    pageSize,
    searchName,
    searchType
  });
/**
 * 添加
 */
export const reqProductAdd = (data: productCollectionType) =>
  ajaxTcbCloud("/manage/product/add", { product: data });
/**
 * 修改
 */
export const reqProductUpdate = (data: productCollectionType) =>
  ajaxTcbCloud("/manage/product/update", { product: data });
/**
 * 修改上线或者下线的状态
 */
export const reqProductUpdateStatus = (_id: string, status: number) =>
  ajaxTcbCloud("/manage/product/updateStatus", { productId: _id, status });
/**
 * 删除
 */
export const reqProductDelete = (_id: string) =>
  ajaxTcbCloud("/manage/product/delete", { _id });
